﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SimpleGameData.GameObjects;
using System.Diagnostics;

namespace SimpleGame.GameObjects.Particles
{
    public class Particle
    {
        #region Fields

        // Create GameObjectData object as we don't read the fields from a xml file
        // but we can use GameObjectData for the fields to avoid unnessecary code duplication
        private GameObjectData particleData = new GameObjectData();
        // true = particle is alive, drawn and updated
        // false = particle is not alive and can be reused
        private bool isAlive;
        // The amount of time the particle can live
        private float life;
        // The amount of time the particle is already alive
        private float lifeTimer;

        #endregion

        #region Properties

        /// <summary>
        /// The particle data read from xml files
        /// </summary>
        public GameObjectData ParticleData
        {
            get { return particleData;  }
            set { particleData = value; }
        }

        /// <summary>
        /// true = particle is alive, drawn and updated
        /// false = particle is not alive and can be reused
        /// </summary>
        public bool IsAlive
        {
            get { return isAlive; }
            set { isAlive = value; }
        }
        /// <summary>
        /// The amount of time the particle can live
        /// </summary>
        public float Life
        {
            get { return life; }
            set { life = value; }
        }

        #endregion

        #region Constructors

        public Particle() 
        {
            isAlive = false;
            lifeTimer = 0.0f;
        }

        public Particle(
            Texture2D texture,
            Vector2 position,
            float velocity,
            Vector2 direction,
            Vector2 scale,
            float rotation,
            Color tintColor,
            float life)
        {
            // Create GameObjectData object as we don't read the fields from a xml file
            // but we can use GameObjectData for the fields to avoid unnessecary code duplication
            particleData = new GameObjectData();

            particleData.Texture = texture;
            particleData.Position = position;
            particleData.Velocity = velocity;
            particleData.Direction = direction;
            particleData.Scale = scale;
            particleData.Rotation = rotation;
            particleData.TintColor = tintColor;
            this.life = life;

            isAlive = false;
            lifeTimer = 0.0f;
        }

        #endregion

        #region Update & Draw Method

        public void Update(GameTime gameTime)
        {
            // only update if particle is alive
            if (isAlive)
            {
                // get elapsed time since last update
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                // add elapsed time to lifetimer
                lifeTimer += elapsed;

                // Move particle with normalized direction vector
                if (particleData.Direction != Vector2.Zero)
                {
                    particleData.Direction.Normalize();
                    particleData.Position += particleData.Direction * particleData.Velocity * elapsed;
                }

                // Check if lifetime of the particle is up, then "kill" particle
                // and reset it's life timer so the particle can be reused
                if (lifeTimer >= life)
                {
                    isAlive = false;
                    lifeTimer = 0.0f;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            // only draw if particle is alive
            if (isAlive)
            {
                spriteBatch.Draw(
                    particleData.Texture,
                    particleData.Position,
                    null,
                    particleData.TintColor,
                    particleData.Rotation,
                    particleData.SpriteCenter,
                    particleData.Scale,
                    SpriteEffects.None,
                    0);
            }
        }

        #endregion
    }
}
